//
//  PayMethodsController.m
//  ManageMoney
//
//  Created by epwk on 15/10/14.
//  Copyright © 2015年 db. All rights reserved.
//

#import "PayMethodsController.h"
#import "ChargeViewController.h"
#import "SearchHistoryController.h"

@interface PayMethodsController ()
@property (strong, nonatomic) NSMutableArray *dataArray;
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation PayMethodsController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    _dataArray = [NSMutableArray array];
    
    [self requestChargeChannelListWithParams:nil];
    
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWith, 1)];
    lineView.backgroundColor = [[UIColor lightGrayColor] colorWithAlphaComponent:0.3];
    [_tableView setTableFooterView:lineView];
    
    
}

//ios8  后cell分割线置顶
-(void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.tableView setSeparatorInset:UIEdgeInsetsZero];
    }
    
    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [self.tableView setLayoutMargins:UIEdgeInsetsZero];
    }
}


#pragma mark - Table view data source
//分区数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

//行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArray.count;
}

//行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 70;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *indentifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:indentifier];
    if (cell==nil) {
//        cell = [[NSBundle mainBundle] loadNibNamed:@"PayHistoryCell" owner:self options:nil][0];
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indentifier];
    }
    
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
    
    NSDictionary *dic = _dataArray[indexPath.row];
//    [cell.imageView sd_setImageWithURL:dic[@"logo"] placeholderImage:nil];
    cell.textLabel.text = CheckNull(dic[@"channelSummary"]);
    cell.textLabel.font = [UIFont boldSystemFontOfSize:16.0];
    [cell.imageView setImage:[UIImage imageNamed:@"ic_yinlian"]];
    
    return cell;
    
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSDictionary *dic = _dataArray[indexPath.row];
    switch (_payType) {
        case 0:
        case 1:
        {
            ChargeViewController *ctr = [[ChargeViewController alloc] initWithNibName:@"ChargeViewController" bundle:nil];
            ctr.payChannelDic = dic;
            ctr.chargeBool = _payType == chargeType?1:0;
            [self.navigationController pushViewController:ctr animated:YES];
        }
            break;
        case 2:
        {
            SearchHistoryController *ctr = [[SearchHistoryController alloc] initWithNibName:@"SearchHistoryController" bundle:nil];
            ctr.payChannelDic = dic;
            ctr.title = @"账户查询";
            [self.navigationController pushViewController:ctr animated:YES];
        }
            break;
            
        default:
            break;
    }
    
}



#pragma mark - RequestAction 充值渠道
-(void)requestChargeChannelListWithParams:(NSDictionary *)pramas
{
    [SVProgressHUD show];
    [[AFNetApiClient sharedClient] requestChargeChannelListWithParams:pramas success:^(AFHTTPRequestOperation *operation , id responseObject) {
        [SVProgressHUD dismiss];
        NSDictionary *dic = responseObject;
        if ([dic[@"responseCode"] intValue] == 200) {
            _dataArray = dic[@"responseObj"];
            
            [_tableView reloadData];
        }else
        {
            [SVProgressHUD showErrorWithStatus:dic[@"responseMessage"]];
        }
    } failure:^(AFHTTPRequestOperation *operation , NSError *error) {
        NSLog(@"%@",error);
        [SVProgressHUD dismiss];
    }];
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
